#include "cclreader.h"

#include <QVariantMap>

#include <qjson/serializer.h>

CCLReader::CCLReader()
{
    tagset = Corpus2::get_named_tagset("nkjp");
}

void CCLReader::readAsJSON(const QString &inputFilePath, QFile &outputFile)
{
    reader = Corpus2::TokenReader::create_path_reader("ccl", tagset, inputFilePath.toStdString());

    QVariantMap doc;

    QString tokens;
    QString base;
    Corpus2::Sentence::Ptr sentence;

    // title
    sentence = reader->get_next_sentence();
    for(size_t i=0; i < sentence->size(); i++){
        tokens += QString::fromStdString(sentence->at(i)->orth_utf8());
        base+= QString::fromStdString(sentence->at(i)->get_preferred_lexeme(tagset).lemma_utf8());
        if(i < sentence->size() - 1)
        {
            tokens += ' ';
        }
        base+=' ';
    }
    doc.insert("title", tokens);

    // content
    tokens.clear();
    while(sentence = reader->get_next_sentence())
        for(size_t i=0; i < sentence->size(); i++){
            tokens += QString::fromStdString(sentence->at(i)->orth_utf8());
            tokens += ' ';
            base+= QString::fromStdString(sentence->at(i)->get_preferred_lexeme(tagset).lemma_utf8());
            base+=' ';
        }
    doc.insert("content", tokens);
    doc.insert("base", base);

    QJson::Serializer serializer;
    QByteArray json = serializer.serialize(doc);
    outputFile.open(QIODevice::WriteOnly);
    outputFile.write(json);
    outputFile.close();
}
